from datetime import datetime

from pytz import timezone

from dao.es_dao.card_dao import find_imei
from service.battery import get_valid_work_time_detail, get_online_redis, get_valid_work_time_detail_by_person
from fastapi import APIRouter
from utils.logger_util import save_data_log_s8_mongo

work_router = APIRouter(prefix='/test/wristband/work', tags=['工作时间'])


@work_router.post("/worktime")
async def get_valid_work_time(data:dict):
    imei_list = data.get('imei_list')
    if  imei_list is None:
        imei_list = await find_imei(data)
    start_date = data['startDate']
    end_date = data['endDate']
    beijing_tz = timezone('Asia/Shanghai')
    start_time = datetime.strptime(start_date, '%Y-%m-%dT%H:%M:%S.%fZ')
    start_time = start_time.astimezone(beijing_tz)
    end_time = datetime.strptime(end_date, '%Y-%m-%dT%H:%M:%S.%fZ')
    end_time = end_time.astimezone(beijing_tz)
    # 将前台传入的字符串 2025-01-01T00:00:00.000Z 转换成 datetime 类型作为mongodb查询条件并像上面加入时区
    return await get_valid_work_time_detail(imei_list, start_time, end_time)


@work_router.post("/worktimePerson")
async def get_valid_work_time(data:dict):
    start_date = data['startDate']
    end_date = data['endDate']
    beijing_tz = timezone('Asia/Shanghai')
    start_time = datetime.strptime(start_date, '%Y-%m-%dT%H:%M:%S.%fZ')
    start_time = start_time.astimezone(beijing_tz)
    end_time = datetime.strptime(end_date, '%Y-%m-%dT%H:%M:%S.%fZ')
    end_time = end_time.astimezone(beijing_tz)
    # 将前台传入的字符串 2025-01-01T00:00:00.000Z 转换成 datetime 类型作为mongodb查询条件并像上面加入时区
    return await get_valid_work_time_detail_by_person(data.get('imei'), start_time, end_time)


@work_router.post("/worktime")
async def get_valid_work_time_by_imei(data:dict):
    imei_list = data.get('imei_list')
    if  imei_list is None:
        imei_list = await find_imei(data)
    start_date = data['startDate']
    end_date = data['endDate']
    beijing_tz = timezone('Asia/Shanghai')
    start_time = datetime.strptime(start_date, '%Y-%m-%dT%H:%M:%S.%fZ')
    start_time = start_time.astimezone(beijing_tz)
    end_time = datetime.strptime(end_date, '%Y-%m-%dT%H:%M:%S.%fZ')
    end_time = end_time.astimezone(beijing_tz)
    # 将前台传入的字符串 2025-01-01T00:00:00.000Z 转换成 datetime 类型作为mongodb查询条件并像上面加入时区
    return await get_valid_work_time_detail(imei_list, start_time, end_time)


@work_router.post("/online")
async def get_online(data:dict):
    imei_list = await find_imei(data)
    # 将前台传入的字符串 2025-01-01T00:00:00.000Z 转换成 datetime 类型作为mongodb查询条件并像上面加入时区
    return await get_online_redis(imei_list)




